﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNASpelProjekt
{       
    /// <summary>
    /// The animation played on death
    /// </summary>
    public class DeathAnimationActor : AnimationActor
    {
        #region member variables
        int offset;
        protected int timeInCurrentFrame;       //time spent in current animation frame
        #endregion

        #region constructors

        /// <summary>
        /// Constructor.
        /// Starts the animation.
        /// </summary>
        /// <param name="texture">The animations initial texture</param>
        /// <param name="x">The animations x-position</param>
        /// <param name="y">The animations y-position</param>
        public DeathAnimationActor(Texture2D texture, int x, int y) : base(texture, x, y)
        {
            offset = 0;
            timeInCurrentFrame = 0;
        }

        #endregion

        /// <summary>
        /// Updates the animation in the game.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            offset += 2;
            timeInCurrentFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeInCurrentFrame > 150)
            {
                if (texture == Game1.deathImage1)
                {
                    texture = Game1.deathImage2;
                }
                else
                {
                    texture = Game1.deathImage1;
                }
                timeInCurrentFrame = 0;
            }
        }

        /// <summary>
        /// Draws the animation to the screen.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// <param name="spriteBatch">A SpriteBatch is used to draw textures.</param>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 drawPos = new Vector2(x, y - offset);            
            spriteBatch.Draw(texture, drawPos, Color.White);

            float xangle = (float)(Math.Cos(Math.PI / 4) * offset);
            float yangle = (float)(Math.Sin(Math.PI / 4) * offset);

            drawPos.X = (float)(X + xangle);
            drawPos.Y = (float)(Y + yangle);
            spriteBatch.Draw(texture, drawPos, Color.White);

            drawPos.Y = (float)(Y - yangle);
            spriteBatch.Draw(texture, drawPos, Color.White);

            drawPos.X = (float)(X - xangle);
            spriteBatch.Draw(texture, drawPos, Color.White);
           
            drawPos.Y = (float)(Y + yangle);
            spriteBatch.Draw(texture, drawPos, Color.White);                   
        }
    }
}
